package com.tqjc.area.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.tqjc.area.entity.Roller;
import com.tqjc.area.entity.SteelPlate;
import com.tqjc.area.mapper.SteelPlateMapper;
import com.tqjc.area.service.CarSemiAutoService;
import com.tqjc.area.service.RollerService;
import com.tqjc.execute.cache.TaskStatus;
import com.tqjc.execute.cache.entity.WorkStatus;
import com.tqjc.execute.exception.TaskRunningException;
import com.tqjc.execute.utils.DateUtils;
import com.tqjc.execute.utils.HttpPLCUtils;
import com.tqjc.execute.work.CraneWorkFactory;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author: 86195
 * @CreateTime: 2024-05-24  15:37
 * @Description: TODO
 * @Version: 1.0
 */
@Service
@Slf4j
public class CarSemiAutoServiceImpl implements CarSemiAutoService {
    @Value("${server_host}")
    private String serverHost;

    @Autowired
    private RestTemplate restTemplate;

    @Resource
    TaskStatus taskStatus;

    @Resource
    CraneWorkFactory craneWorkFactory;

    @Autowired
    private SteelPlateMapper steelPlateMapper;

    @Autowired
    private RollerService rollerService;


    @Override
    public void move1Plus(String craneType,String task) throws Exception {
        try {

            //清所有任务标志
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志D",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志D",false);
            }
            //写入半自动标志
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","半自动标志",false);
            //写入垛位
            if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
            }
            //写入任务
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","下料标志A",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","上料标志A",false);
            }
            //写入任务启动
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","任务启动",false);
            boolean isFinished = false;
            String isFinishStr="";
            Date startDate=new Date();
            while (!isFinished) {
                boolean overTime= DateUtils.isOverTimeByNow(startDate,100000);//超过100秒未完成，退出等待。
                if(overTime){

                    WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                    status.setUnnormalStatus("等待任务完成超时");
                    taskStatus.setWorkStatusByCraneType(craneType, status);
                    boolean isStartLogo= craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
                    if(!isStartLogo){
                        break;
                    }
                }
                if("offline".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"下料A完成",false);
                }else if("online".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"上料A完成",false);
                }
                if ("true".equalsIgnoreCase(isFinishStr)) {
                    //清任务信号
                    if("offline".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志A",false);
                    }else if("online".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志A",false);
                    }
                    //清任务启动信号
                    HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务启动",false);
                    if("offline".equalsIgnoreCase(task)){
                        //清下料A完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料A完成",false);
                    }else if("online".equalsIgnoreCase(task)){
                        //清上料A完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料A完成",false);
                    }
                    isFinished = true;
                    log.info("PLC反馈【{}】天车自由半自动步骤1任务完成",craneType);
                }
                String taskFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, craneType, "任务中止", false);
                if("true".equalsIgnoreCase(taskFlag)){
                    log.info("天车执行自由半自动步骤1时，监听到任务中止信号");
                    throw new TaskRunningException("任务人工停止",craneType);
                }
            }
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus("");
            taskStatus.setWorkStatusByCraneType(craneType, status);
        } catch (Exception e) {
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus(e.getMessage());
            taskStatus.setWorkStatusByCraneType(craneType, status);
            log.error("天车【{}】自由半自动移动到辊道步骤异常",craneType,e);
            throw new Exception("天车【"+ craneType +"】自由半自动移动到辊道步骤异常");
        }


    }

    @Override
    public void suck2Plus(String craneType,String setSteelNo,String task) throws Exception {

        try {

            if(StringUtils.isNotEmpty(setSteelNo)){
                WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                status.setUnnormalStatus("执行自由半自动吸板任务异常，没有输入对应的钢板号");
                taskStatus.setWorkStatusByCraneType(craneType, status);
                return;
            }

            SteelPlate steelPlate = steelPlateMapper.selectOne(new LambdaQueryWrapper<SteelPlate>().eq(SteelPlate::getSteelId, setSteelNo));
            if(steelPlate == null){
                WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                status.setUnnormalStatus("执行自由半自动吸板任务异常，没有找到对应的钢板详情信息");
                taskStatus.setWorkStatusByCraneType(craneType, status);
                return;
            }

            /*Task task4Z= WorkConst.cacheTaskMap.get(craneType+"_task");
            Integer height=0;
            if(task4Z!=null){
                List<SteelPlate> steels=task4Z.getSteels4Offline();
                if("offline".equals(getTask().getType())){
                    //如果是suck多块，z轴要加上（已吸钢板-1）和要吸钢板的厚度
                    for(int i=1;i<steels.size();i++){ //吸第一块的高度在生成task时，就已经加到z轴上了，所以i从1开始；
                        height+= steels.get(i).getThickness();
                    }
                }
            }*/

            //写入辊道锁紧信号
            if("offline".equalsIgnoreCase(task)){
                Roller roller= rollerService.getByName(craneType);
                String[] lockArr=roller.getLockSignal().split("-");
                //清一级辊道锁定信号
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr[0],true);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
            }

            //清所有任务标志
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志D",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志D",false);
            }
            //写入半自动标志
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","半自动标志",false);

            //写入任务
            if("offline".equalsIgnoreCase(task)){
                //写钢板信息给天车
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.24",HttpPLCUtils.dataTypeList.get("int16"),steelPlate.getLength()+"","任务钢板长",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.26",HttpPLCUtils.dataTypeList.get("int16"),steelPlate.getWidth()+"","任务钢板宽",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.28",HttpPLCUtils.dataTypeList.get("int16"),steelPlate.getThickness()+"","任务钢板厚",false);

                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","下料标志B",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","上料标志B",false);
            }
            //写入任务启动
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","任务启动",false);
            boolean isFinished = false;
            String isFinishStr="";
            Date startDate=new Date();
            while (!isFinished) {
                boolean overTime= DateUtils.isOverTimeByNow(startDate,100000);//超过100秒未完成，退出等待。
                if(overTime){
                    //log.error("等待{}天车半自动步骤2任务完成超时",getTask().getCraneType());
                    //throw new Exception("等待"+getTask().getCraneType()+"天车半自动步骤2任务完成超时");
                    WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                    status.setUnnormalStatus("等待任务完成超时");
                    taskStatus.setWorkStatusByCraneType(craneType, status);
                    boolean isStartLogo= craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
                    if(!isStartLogo){
                        break;
                    }
                }
                if("offline".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"下料B完成",false);
                }else if("online".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"上料B完成",false);
                }
                if ("true".equalsIgnoreCase(isFinishStr)) {
                    //清任务信号
                    if("offline".equalsIgnoreCase(task)){
                        Roller roller= rollerService.getByName(craneType);
                        String[] lockArr=roller.getLockSignal().split("-");
                        //清一级辊道锁定信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr[0],true);
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
                    }else if("online".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
                    }
                    //清任务启动信号
                    HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务启动",false);
                    if("offline".equalsIgnoreCase(task)){
                        //清下料B完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料B完成",false);
                    }else if("online".equalsIgnoreCase(task)){
                        //清上料B完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料B完成",false);
                    }
                    isFinished = true;
                    log.info("PLC反馈【{}】天车自由半自动步骤2任务完成",task);
                }
                String taskFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, craneType, "任务中止", false);
                if("true".equalsIgnoreCase(taskFlag)){
                    log.info("天车执行自由半自动步骤2时，监听到任务中止信号");
                    throw new TaskRunningException("任务人工停止",craneType);
                }
            }
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus("");
            taskStatus.setWorkStatusByCraneType(craneType, status);
        } catch (Exception e) {
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus(e.getMessage());
            taskStatus.setWorkStatusByCraneType(craneType, status);
            log.error("天车【{}】自由半自动吸板步骤异常",craneType,e);
            throw new Exception("天车【"+ craneType +"】自由半自动吸板步骤异常");
        }

    }

    @Override
    public void move3Plus(String craneType,String task,String stockNo) throws Exception {

        try {

            //清所有任务标志
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志D",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志D",false);
            }
            //写入半自动标志
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","半自动标志",false);

            //写入垛位
            if("offline".equalsIgnoreCase(task)){
                if("11C北".equals(craneType)){
                    if("1".equals(stockNo)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位2",false);
                    }else {
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位2",false);
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位1",false);
                    }
                }else if("11C南".equals(craneType)){
                    if("1".equals(stockNo)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位2",false);

                    }else {
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.9",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位2",false);
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","垛位1",false);
                    }
                }
                else {
                    HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","垛位1",false);
                }

            }
            //写入任务
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","下料标志C",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","上料标志C",false);
            }
            //写入任务启动
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","任务启动",false);

            boolean isFinished = false;
            String isFinishStr="";
            Date startDate=new Date();
            while (!isFinished) {
                boolean overTime= DateUtils.isOverTimeByNow(startDate,100000);//超过100秒未完成，退出等待。
                if(overTime){
                    //log.error("等待{}天车半自动步骤3任务完成超时",getTask().getCraneType());
                    //throw new Exception("等待"+getTask().getCraneType()+"天车半自动步骤3任务完成超时");
                    WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                    status.setUnnormalStatus("等待任务完成超时");
                    taskStatus.setWorkStatusByCraneType(craneType, status);
                    boolean isStartLogo= craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
                    if(!isStartLogo){
                        break;
                    }
                }
                if("offline".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"下料C完成",false);
                }else if("online".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"上料C完成",false);
                }
                if ("true".equalsIgnoreCase(isFinishStr)) {
                    //清任务信号
                    if("offline".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志C",false);
                    }else if("online".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志C",false);
                    }
                    //清任务启动信号
                    HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务启动",false);
                    if("offline".equalsIgnoreCase(task)){
                        //清下料C完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料C完成",false);
                    }else if("online".equalsIgnoreCase(task)){
                        //清上料C完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料C完成",false);
                    }
                    isFinished = true;
                    log.info("PLC反馈【{}】天车自由半自动步骤3任务完成",craneType);
                }
                String taskFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, craneType, "任务中止", false);
                if("true".equalsIgnoreCase(taskFlag)){
                    log.info("天车执行自由半自动步骤3时，监听到任务中止信号");
                    throw new TaskRunningException("任务人工停止",craneType);
                }
            }
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus("");
            taskStatus.setWorkStatusByCraneType(craneType, status);
        } catch (Exception e) {
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus(e.getMessage());
            taskStatus.setWorkStatusByCraneType(craneType, status);
            log.error("天车【{}】自由半自动移动到垛位步骤异常",craneType,e);
            throw new Exception("天车【"+ craneType +"】自由半自动移动到垛位步骤异常");
        }

    }

    @Override
    public void place4Plus(String craneType,String task) throws Exception {

        try {
            /*Task task4Z= WorkConst.cacheTaskMap.get(craneType+"_task");
            Integer height=0;
            if(task4Z!=null){
                if("online".equals(getTask().getType())){
                    height=task4Z.getSteel4Online().getThickness();
                }else {
                    List<SteelPlate> steels=task4Z.getSteels4Offline();
                    for(int i=0;i<steels.size();i++){
                        height+= steels.get(i).getThickness();
                    }
                }
            }*/
            //清所有任务标志
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.6",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志D",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志A",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.3",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志B",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.4",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志C",false);
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志D",false);
            }
            //写入半自动标志
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","半自动标志",false);

            Roller roller= rollerService.getByName(craneType);
            String[] lockArr=roller.getLockSignal().split("-");
            //写入任务
            if("offline".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","下料标志D",false);
            }else if("online".equalsIgnoreCase(task)){
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","上料标志D",false);


                //写一级辊道锁定信号
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"TRUE",lockArr[0],true);
                //向天车写辊道锁定信息信号
                HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.10",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","辊道锁定",false);

            }
            //写入任务启动
            HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"TRUE","任务启动",false);

            boolean isFinished = false;
            String isFinishStr="";
            Date startDate=new Date();
            while (!isFinished) {
                boolean overTime= DateUtils.isOverTimeByNow(startDate,100000);//超过100秒未完成，退出等待。
                if(overTime){
                    //log.error("等待{}天车半自动步骤4任务完成超时",getTask().getCraneType());
                    //throw new Exception("等待"+getTask().getCraneType()+"天车半自动步骤4任务完成超时");
                    WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
                    status.setUnnormalStatus("等待任务完成超时");
                    taskStatus.setWorkStatusByCraneType(craneType, status);
                    boolean isStartLogo= craneWorkFactory.getCraneWorkImpl(craneType).getData().getSemiAutoRunFlag();
                    if(!isStartLogo){
                        break;
                    }
                }
                if("offline".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"下料D完成",false);
                }else if("online".equalsIgnoreCase(task)){
                    isFinishStr= HttpPLCUtils.plcReadbc(restTemplate,serverHost,craneType,"上料D完成",false);
                }
                if ("true".equalsIgnoreCase(isFinishStr)) {
                    //清任务信号
                    if("offline".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料标志D",false);
                    }else if("online".equalsIgnoreCase(task)){
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.6.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料标志D",false);
                    }
                    //清任务启动信号
                    HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.2",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","任务启动",false);
                    if("offline".equalsIgnoreCase(task)){
                        //清下料D完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.8.1",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","下料D完成",false);
                    }else if("online".equalsIgnoreCase(task)){
                        //清上料D完成信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.7.5",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","上料D完成",false);

                        //清一级辊道锁定信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,lockArr[1],HttpPLCUtils.dataTypeList.get("bool"),"FALSE",lockArr[0],true);
                        //清天车辊道锁定信息信号
                        HttpPLCUtils.plcWritebcPlus(restTemplate,serverHost,craneType,"DB20.10",HttpPLCUtils.dataTypeList.get("bool"),"FALSE","辊道锁定",false);

                    }
                    isFinished = true;
                    log.info("PLC反馈【{}】天车自由半自动任务放板任务完成",task);
                }
                String taskFlag = HttpPLCUtils.plcReadbc(restTemplate, serverHost, craneType, "任务中止", false);
                if("true".equalsIgnoreCase(taskFlag)){
                    log.info("天车执行自由半自动步骤4时，监听到任务中止信号");
                    throw new TaskRunningException("任务人工停止",craneType);
                }
            }
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus("");
            taskStatus.setWorkStatusByCraneType(craneType, status);
        } catch (Exception e) {
            WorkStatus status = taskStatus.getWorkStatusByCraneType(craneType);
            status.setUnnormalStatus(e.getMessage());
            taskStatus.setWorkStatusByCraneType(craneType, status);
            log.error("天车【{}】自由半自动放板步骤异常",craneType,e);
            throw new Exception("天车【"+ craneType +"】自由半自动放板步骤异常");
        }

    }
}
